package com.cxj.cxj_test_cloud.utils;

import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class ToastUtil {

    public static void show(CharSequence text){
        if (text == null){
            return;
        }
        try {
            if (Looper.myLooper() != Looper.getMainLooper()){
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        showToast(text);
                    }
                });
            }else {
                showToast(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void show(int resId){
        if (resId <= 0){
            return;
        }
        try {
            if (Looper.myLooper() != Looper.getMainLooper()){
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        showToast(resId);
                    }
                });
            }else {
                showToast(resId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showShort(CharSequence text){
        if (text == null){
            return;
        }
        try {
            if (Looper.myLooper() != Looper.getMainLooper()){
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        showToastShort(text);
                    }
                });
            }else {
                showToastShort(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showShort(int resId){
        if (resId <= 0){
            return;
        }
        try {
            if (Looper.myLooper() != Looper.getMainLooper()){
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        showToastShort(resId);
                    }
                });
            }else {
                showToastShort(resId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void showToast(CharSequence text) {
        Toast toast = Toast.makeText(SswlApplication.getAppComponent().context(), "", Toast.LENGTH_LONG);
        toast.setText(text);
        toast.show();
    }

    public static void showToast(int resId){
        Toast toast = Toast.makeText(SswlApplication.getAppComponent().context(), "", Toast.LENGTH_LONG);
        toast.setText(resId);
        toast.show();
    }

    public static void showToastShort(CharSequence text){
        Toast toast = Toast.makeText(SswlApplication.getAppComponent().context(), "", Toast.LENGTH_SHORT);
        toast.setText(text);
        toast.show();
    }

    public static void showToastShort(int resId){
        Toast toast = Toast.makeText(SswlApplication.getAppComponent().context(), "", Toast.LENGTH_SHORT);
        toast.setText(resId);
        toast.show();
    }
}
